package com.example.m_kartupay;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.HashMap;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.m_kartupay.LoginActivity.LoginLogOut;
import com.example.m_kartupay.MainListActivity.JSONAsyncTask;
import com.example.m_kartupay.library.DatabaseHandler;
import com.example.m_kartupay.library.UserFunctions;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.TextView;
import android.widget.EditText;

public class PurchaseInvoiceActivity extends Activity {
	String url,notiket;
	
	Button btnPurchaseInvoice;
	TextView  tvKodeBooking;
	TextView tvNamaCustomer;
	TextView tvAlamat;
	TextView tvprice;
	TextView jumlahTiket;
	TextView tvHargaTotal;
	EditText edKodeBooking;
	LinearLayout lyTiketDetail;

	String  KodeBooking,NamaCustomer,Alamat,price, jmlTiket,HargaTotal;
	@Override
	  protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.purchase_invoice);
	    tvKodeBooking = (TextView)findViewById(R.id.tvKodeBooking);
	    tvNamaCustomer = (TextView)findViewById(R.id.tvNamaCustomer);
	    tvAlamat = (TextView)findViewById(R.id.tvAlamat);
	    tvprice = (TextView)findViewById(R.id.tvprice);
	    jumlahTiket = (TextView)findViewById(R.id.jumlahTiket);
	    tvHargaTotal = (TextView)findViewById(R.id.tvHargaTotal);
	    btnPurchaseInvoice = (Button)findViewById(R.id.btnPurchaseScan);
	    lyTiketDetail = (LinearLayout)findViewById(R.id.tiketDitel);
	    edKodeBooking = (EditText)findViewById(R.id.edKodeBooking);
	    
	    
	    btnPurchaseInvoice.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				
				 url = "http://118.97.213.239/reservasi_development/kartuPay.php?kode_booking='82583006'";
				 new JSONAsyncTask().execute("http://118.97.213.239/reservasi_development/kartuPay.php");
			}
		});
	    
	 }
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
		
		ProgressDialog dialog;
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new ProgressDialog(PurchaseInvoiceActivity.this);
			dialog.setMessage("Loading, please wait");
			dialog.setTitle("Connecting server");
			dialog.show();
			dialog.setCancelable(false);
		}
		
		@Override
		protected Boolean doInBackground(String... urls) {
			
			Boolean status = false;
			String kode_booking = "82583006";
			notiket = null;
		    notiket = edKodeBooking.getText().toString();
			Log.d("kode_booking",kode_booking);
		
			UserFunctions userFunction = new UserFunctions();
			String json = userFunction.getTicketShuttle(url, notiket);
			Log.d("JSON SHUTTLE", json.toString());
			
			
			try {
				JSONObject jsonObj = new JSONObject(json);
				
				
				Log.d("Object Shuttle",jsonObj.toString());
				JSONObject dataObject = jsonObj.getJSONObject("data");
				Log.d("Object Data",dataObject.toString());
				JSONObject dataNOL = dataObject.getJSONObject("0");
				Log.d("Kode Booking",dataNOL.getString("kode_booking"));
				
				Log.d("ResponseStatus",jsonObj.getString("responseStatus").toString());
				if(jsonObj.getString("responseStatus").toString().equals("Success"))
				{
					KodeBooking=dataNOL.getString("kode_booking");
					NamaCustomer=dataNOL.getString("nama");
					Alamat=dataNOL.getString("alamat");
					price=dataNOL.getString("hargatotal");
					jmlTiket=dataNOL.getString("jmltiket");
					HargaTotal=dataNOL.getString("hargatotal");
					status = true;
				}						
				else {
					status = false;
				}
				
				
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			return status;
			
		}
		
		protected void onPostExecute(Boolean result) {
			dialog.cancel();
			
			if(result == false)
			{
				Toast.makeText(getApplicationContext(), "Invalid Booking Code", Toast.LENGTH_LONG).show();
				tvKodeBooking.setText("");
				tvNamaCustomer.setText("");
				tvAlamat.setText("");
				tvprice.setText("");
				jumlahTiket.setText("");
				tvHargaTotal.setText("");
				lyTiketDetail.setVisibility(View.GONE);
                       
			}
			if(result == true)
			{				
				tvKodeBooking.setText("");
				tvNamaCustomer.setText("");
				tvAlamat.setText("");
				tvprice.setText("");
				jumlahTiket.setText("");
				tvHargaTotal.setText("");
				
				tvKodeBooking.setText(KodeBooking);
				tvNamaCustomer.setText(NamaCustomer);
				tvAlamat.setText(Alamat);
				tvprice.setText(price);
				jumlahTiket.setText(jmlTiket);
				tvHargaTotal.setText(HargaTotal);
				lyTiketDetail.setVisibility(View.VISIBLE);;
				
				
				
			}
		}
	}

}
